package com.kitty.game.fight.zhenfa;

import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.StaticParam;
import com.kitty.game.attribute.AttrService;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.message.RespAttackSkillLightEffect;
import com.kitty.game.fight.message.RespCombatUpdateImprovement;
import com.kitty.game.fight.util.FightMessageUtil;
import com.kitty.game.fight.zhenfa.message.BattleArrayInfoResp;
import com.kitty.game.item.config.ChangeCard;
import com.kitty.game.role.model.Role;
import com.kitty.game.school.SchoolDataPool;
import com.kitty.mina.message.MessagePusher;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

@Service
public class ZhenFaService {

    /***
     * 获取队伍中所有的变身卡，
     * 判断组成什么阵法
     */
    public static ZhenFaInfo getChangeCardZhen(List<FightObject> fightObjects) {
        if (!fightObjects.get(0).isPlayer()) {
            return null;
        }
        List<ChangeCard> names = new ArrayList<>();
        List<ChangeCard> listA = new ArrayList();
        List<ChangeCard> listB = new ArrayList();
        //获取队伍所有变身卡，放入集合中
        for (FightObject fightObject : fightObjects) {
            if (fightObject.isPlayer()) {
                Role role = fightObject.getRole();
                ChangeCard changeCardName = role.getChangeCard();
                if (changeCardName != null) {
                    names.add(changeCardName);
                }
            }
        }
        //按照变身卡名字 进行分组。
        Map<String, List<ChangeCard>> map = new HashMap<>();
        for (ChangeCard changeCard : names) {
            List<ChangeCard> cards = map.computeIfAbsent(changeCard.getName(), k -> new ArrayList<>());
            cards.add(changeCard);
        }
        if (map.size() > 2) {
            return null;
        }
        if (map.size() == 0) {
            return null;
        }
        //取队伍中变身卡数量大于等于三的
        for (Map.Entry<String, List<ChangeCard>> stringListEntry : map.entrySet()) {
            if (stringListEntry.getValue().size() >= 3) {
                listB = stringListEntry.getValue();
            } else {
                listA = stringListEntry.getValue();
            }
        }
        String changeCardNum = getChangeCardNum(listA, listB, names);
        if (StringUtils.isNotEmpty(changeCardNum)) {
            ZhenFaInfo zhenFaInfo = new ZhenFaInfo();
            zhenFaInfo.setZhenfa(changeCardNum);
            int polar = -1;
            if (listA.size() > listB.size()) {
                polar = listA.get(0).getPolar();
            } else {
                polar = listB.get(0).getPolar();
            }
            //若变身卡相性为0，则随机一种相性
            if (polar == 0) {
                polar = ThreadLocalRandom.current().nextInt(5) + 1;
            }
            zhenFaInfo.setPolar(polar);
            return zhenFaInfo;
        }
        return null;
    }

    /**
     * 发送阵法克制关系
     *
     * @param fight
     * @param firend
     * @param opponent
     * @param isA
     */
    public void sendBattleArray(Fight fight, FightObject fightObject, ZhenFaInfo firend, ZhenFaInfo opponent, boolean isA) {
        if (firend == null) {
            firend = new ZhenFaInfo();
            firend.setPolar(0);
        }
        if (opponent == null) {
            opponent = new ZhenFaInfo();
            opponent.setPolar(0);
        }
        if (fightObject.isPlayer()) {
            Role role = fightObject.getRole();
            byte changeCardType = ZhenFaService.getChangeCardType(firend, opponent);
            if (changeCardType == 1 || changeCardType == 0) {
                fightObject.setIsZhenfa(1);
                fightObject.setZhenfaPolar((short) firend.getPolar());
                this.fightChangeCardExtra(fight, fightObject, firend.getZhenfa());
            } else {
                fightObject.setIsZhenfa(0);
            }
            BattleArrayInfoResp battleArrayInfoResp = new BattleArrayInfoResp();
            battleArrayInfoResp.setFrientPolar((byte) firend.getPolar());
            battleArrayInfoResp.setOpponentPolar((byte) opponent.getPolar());
            battleArrayInfoResp.setType(changeCardType);
            FightMessageUtil.sendFightPackage(fight, battleArrayInfoResp, isA);
        }
    }

    /**
     * 战斗变身卡增益属性包
     */
    public void fightChangeCardExtra(Fight fight, FightObject fightObject, String zhenFaName) {
        if (fightObject.isPlayer()) {
            Role role = fightObject.getRole();
            RespCombatUpdateImprovement respCombatUpdateImprovement = new RespCombatUpdateImprovement();
            respCombatUpdateImprovement.setId(fightObject.getId());
            List<FiedValue> list = new ArrayList<>();
            respCombatUpdateImprovement.setList(list);
            /**把变身卡的属性取出来 由于变身卡属性是百分比增加 所以放到最后面*/
            ChangeCard changeCard = fightObject.getChangeCard();
            String name = StaticParam.getPolarByNumber(fightObject.getZhenfaPolar());
//            String msg = "触发"+name+"相性阵法：#R"+zhenFaName+"#n，你使用的#R"+changeCard.getName()+"#n对应的阵法效果已生效！";
            AttrService attrService = SpringUtils.getBean(AttrService.class);
            if (changeCard != null) {
                Map<Short, Integer> cardFields = new HashMap<>();
                changeCard.getBattleAttrib().forEach(cardAttrib -> {
                    short fieldId = attrService.getFieldIdByName(cardAttrib.getFieldName());
                    cardFields.put(fieldId, cardAttrib.getValue());
                });
                String addName = "";
                Integer value = 0;
                for (Map.Entry<Short, Integer> fields : cardFields.entrySet()) {
                    Short fieldId = fields.getKey();
                    value = fields.getValue();
                    if (fieldId == 3) {
                        //物伤
                        value = role.getExtraPhyPower() * value / 100;
                        addName = "物理伤害";
                    }
                    if (fieldId == 10) {
                        //法伤
                        value = role.getExtraMagPower() * value / 100;
                        addName = "法术伤害";
                    }
                    if (fieldId == 14) {
                        //速度
                        value = role.getExtraSpeed() * value / 100;
                        addName = "速度";
                    }
                    if (fieldId == 8) {
                        //防御
                        value = role.getExtraDefense() * value / 100;
                        addName = "防御";
                    }
                    if (fieldId == 7) {
                        //气血
                        value = role.getExtraLife() * value / 100;
                        addName = "气血";
                    }
                    if (fieldId == 169) {
                        //忽视所有抗异常
                    }

                    list.add(new FiedValue((short) fieldId, value));
                }
                if (changeCard != null) {
                    String msg = "触发" + name + "相性阵法：#R" + zhenFaName + "#n，你使用的#R" + changeCard.getName() + "#n对应的阵法效果#R"+addName+"+"+value+"%#n已生效！";
                    MessagePusher.pushMessage(role, new RespNotifyMiscEx(msg));
                } else {
                    String msg = "触发" + name + "相性阵法：#R" + zhenFaName + "#n";
                    MessagePusher.pushMessage(role, new RespNotifyMiscEx(msg));
                }

            }
            FightMessageUtil.sendFightPackage(fight, respCombatUpdateImprovement);
        }


    }

    /**
     * 发送阵法动画特效
     *
     * @param fight
     * @param fightObject
     * @param polar
     * @param type
     */
    public static void sendZhenDaEffect(Fight fight, FightObject fightObject, int polar, int type) {
        fightObject.setZhenfaPolar((short) polar);
        if (polar == 1) {
            polar = 8001;
        } else if (polar == 2) {
            polar = 8009;
        } else if (polar == 3) {
            polar = 8005;
        } else if (polar == 4) {
            polar = 8007;
        } else if (polar == 5) {
            polar = 8003;
        }
        if (type == 2) {
            polar += 1;
        }
        FightMessageUtil.sendFightPackage(fight, new RespAttackSkillLightEffect(fightObject.getId(), (short) polar, 0, ""));
    }

    /**
     * 获取相性克制关系
     *
     * @param friend
     * @param opponent
     * @return
     */
    public static byte getChangeCardType(ZhenFaInfo friend, ZhenFaInfo opponent) {
        //队友的相性
        int polar = friend.getPolar();
        //克制队友相性    的  相性
        Byte kezhi = SchoolDataPool.taskToFightMap.get((byte) polar) == null ? 0 : SchoolDataPool.taskToFightMap.get((byte) polar);
        if (kezhi == opponent.getPolar()) {
            return -1;
            //队友被克制
        }
        //克制对方相性    的  相性
        Byte kezhi2 = SchoolDataPool.taskToFightMap.get((byte) opponent.getPolar()) == null ? 0 : SchoolDataPool.taskToFightMap.get((byte) opponent.getPolar());
        if (kezhi2 == friend.getPolar()) {
            //对方被克制
            return 1;
        }
        if (polar == 0) {
            return -2;
        }
        return 0;
    }

    public static String getChangeCardNum(List cardA, List cardB, List<ChangeCard> cards) {
        if (cards.size() == 5) {
            if (cardA.size() == 4 || cardB.size() == 4) {
                return "地载阵";
            }
            if (cardA.size() == 5 || cardB.size() == 5) {
                return "天覆阵";
            }
            if (cardA.size() == 3 || cardB.size() == 3) {
                return "风扬阵";
            }
        } else if (cards.size() == 4) {
            if (cardA.size() == 4 || cardB.size() == 4) {
                return "云垂阵";
            }
            if (cardA.size() == 3 || cardB.size() == 3) {
                return "龙飞阵";
            }
        } else if (cards.size() == 3) {
            if (cardA.size() == 3 || cardB.size() == 3) {
                return "虎翼阵";
            }
            if (cardA.size() == 2 || cardB.size() == 2) {
                return "鸟翔阵";
            }
        } else if (cards.size() == 2) {
            if (cardA.size() == 2 || cardB.size() == 2) {
                return "蛇蟠阵";
            }
        }


        return "";
    }
}
